package com.zeratul.common.satoken.config;

import cn.dev33.satoken.jwt.StpLogicJwtForMixin;
import cn.dev33.satoken.stp.StpLogic;
import com.zeratul.common.core.factory.YmlPropertySourceFactory;
import com.zeratul.common.satoken.config.prop.ZeratulSaTokenProperties;
import com.zeratul.common.satoken.log.SaLogForSlf4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

@AutoConfiguration
@PropertySource(value = "classpath:common-satoken.yml", factory = YmlPropertySourceFactory.class)
@EnableConfigurationProperties(ZeratulSaTokenProperties.class)
@Slf4j
public class ZeratulSaTokenAutoConfiguration {

    /**
     * SaToken日志转Slf4j日志
     */
    @Bean
    @ConditionalOnProperty(prefix = "sa-token", name = "is-log", havingValue = "true")
    public SaLogForSlf4j saLogForSlf4j() {
        return new SaLogForSlf4j();
    }

    /**
     *  Sa-Token 整合 jwt (Mixn 混合模式)
     */
    @Bean
    @ConditionalOnProperty(prefix = "sa-token", name = "token-model", havingValue = "JWT_MIXIN")
    public StpLogic getStpLogicJwtForMixin() {
        return new StpLogicJwtForMixin();
    }
}
